<?php

namespace app\common\model\Post;

use think\Model;
// 引用软删除
use traits\model\SoftDelete;

class Comment extends Model
{
    // TODO 评论表
    protected $table = "pre_comment";

    // 自动写入时间
    protected $autoWriteTimestamp = true;

    // 创建时间 => 注册时间
    protected $createTime = 'createtime';

    // 更新时间
    protected $updateTime = false;

    // 软删除
    use SoftDelete;
    protected $deleteTime = 'deletetime';

    // TODO 
    protected $append = [
        'createtime_text',
        // 'like_count'
    ];

    public function getCreatetimeTextAttr($value, $data)
    {
        $createtime_text = date('Y-m-d H:i:s', $data['createtime']);
        return $createtime_text;
    }

    public function getLikeCountAttr($value, $data)
    {
        $count = $data['like'] ? count(explode(',', $data['like'])) : 0;
        return $count;
    }

    public function getAcceptTextAttr($value, $data)
    {
        if ($data['accept'] > 0) {
            return '已采纳';
        } else {
            return '未采纳';
        }
    }

    // TODO 连表查询pre_post
    public function post()
    {
        return $this->belongsTo('app\common\model\Post\Post', 'postid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    // TODO 连表查询pre_user
    public function user()
    {
        return $this->belongsTo('app\common\model\User\User', 'userid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
